package com.ruoyi.common.utils;

import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;
import java.util.function.Predicate;

public class BigDecimalUtil {

    public static <T> BigDecimal sum(List<T> list, Function<T, BigDecimal> mapper) {
        Objects.requireNonNull(list);
        Objects.requireNonNull(mapper);
        return list.stream().map(mapper).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add);
    }

    public static <T> BigDecimal sum(List<T> list, Function<T, BigDecimal> mapper, Predicate<? super T> filter) {
        Objects.requireNonNull(list);
        Objects.requireNonNull(mapper);
        return list.stream().filter(filter).map(mapper).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add);
    }

}
